import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class RekeningAanmakenGUI extends JFrame implements ActionListener{
	
	private JLabel l1,l2,error;
	private JTextField tf1;
	private JComboBox combo;
	private JButton ok,cancel;
	private JPanel p1,p2;
	private Bank bank;
	private Klant klant;
	
	public RekeningAanmakenGUI(Klant kl, Bank b){
		//start
		klant = kl;
		bank = b;
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		
		//define and insert panels
		p1 = new JPanel();
		p2 = new JPanel();
		p1.setLayout(new GridLayout(2,2));
		p2.setLayout(new FlowLayout());
		c.add(p1, BorderLayout.CENTER);
		c.add(p2, BorderLayout.SOUTH);
		
		//define components
		l1 = new JLabel("Rekening nummer: ");
		l2 = new JLabel("Rekening Type: ");
		error = new JLabel(" ");
		error.setForeground(Color.RED);
		tf1 = new JTextField("",5);
		combo = new JComboBox(b.alleRekeningTypes());
		ok = new JButton("Create new");
		cancel = new JButton("Cancel");
		
		//insert components
		c.add(error, BorderLayout.NORTH);
		p1.add(l1);
		p1.add(tf1);
		p1.add(l2);
		p1.add(combo);
		p2.add(ok);
		p2.add(cancel);
		
		//action listeners
		ok.addActionListener(this);
		cancel.addActionListener(this);
		
		//end
		setSize(new Dimension(300,130));
		setTitle("Rekening aanmaken");
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
	}
	
	public void actionPerformed(ActionEvent e){
		String naam;
		if(e.getSource() == ok){
			if(!tf1.equals("")){
				naam = (String) combo.getSelectedItem();
				HashMap mp;
				mp = bank.rekeningTypes();
				if(mp.containsKey(naam)){
					try{
					bank.voegToe(new Rekening(Integer.parseInt(tf1.getText()),klant,(RekeningType)mp.get(naam)));
					error.setText("De Rekening: " + tf1.getText() + " is toegevoegd");
					tf1.setText("");
					}
					
					catch(NumberFormatException fe){
						error.setText("Vul alleen cijfers in!");
					}
				}
				else
					error.setText("Dat Rekening type bestaat niet!");
			}
			else
				error.setText("Vul Rekeningnummer in!");
		}
		else
			dispose();
	}
	
}
